package com.tg.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.tg.app.R;

import java.util.HashMap;

/**
 * Describe:
 * Created by Rance Rang on 2020-02-26.
 * <p>
 * Copyright (c) Tange.Inc All rights reserved.
 */
public class DetectionAreaAdapter  extends RecyclerView.Adapter<DetectionAreaAdapter.DetectionHolder> {

    private HashMap<Integer,Boolean> areaList ;
    private Context context;

    private View.OnClickListener onClickListener;

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public  DetectionAreaAdapter(HashMap<Integer,Boolean> areaList ){
        this.areaList = areaList;
    }
    @NonNull
    @Override
    public DetectionHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.layout_detection_area_item,parent,false);
        return new DetectionHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DetectionHolder holder, final int position) {
      final boolean  isCheck  =  areaList.containsKey(position)?areaList.get(position):false;
        if (isCheck){
            holder.textView.setBackgroundColor(context.getResources().getColor(R.color.detecton_area_color));
        }else {
            holder.textView.setBackgroundColor(context.getResources().getColor(R.color.transparent));
        }
        holder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                 areaList.put(position,!isCheck);
               notifyItemChanged(position);
               if (onClickListener!=null){
                   onClickListener.onClick(view);
               }
            }
        });

    }

    public HashMap<Integer, Boolean> getAreaList() {
        return areaList;
    }

    @Override
    public int getItemCount() {
        return areaList==null?0:25;
    }

    public class DetectionHolder extends RecyclerView.ViewHolder {
        TextView  textView ;
        public DetectionHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv_detection_area_item);
        }
    }
}
